# -*- coding: utf-8 -*-
import os
from tools.base import CmdTask,FileUtils,PrintUtils,ChooseTask
from tools.base import encoding_utf8,osversion,osarch,distribution
from tools.base import run_tool_file,download_tools
from tools.base import config_helper

url_prefix = os.getcwd()

base_url = url_prefix+'tools/base.py'

tools ={
    1: {'tip':'一键安装:ROS (支持ROS和ROS2)',                 'type':0,     'tool':url_prefix+'tools/tool_install_ros.py' ,'dep':[4,5] },
    2: {'tip':'一键安装:ROS+Docker (支持所有版本的ROS/ROS2,十分推荐!!!)',                'type':0,    'tool':url_prefix+'tools/tool_install_ros_with_docker.py' ,'dep':[6,8] },
    3: {'tip':'一键配置:ROS环境 (快速更新ROS环境设置,自动生成环境选择)',     'type':2,     'tool':url_prefix+'tools/tool_config_rosenv.py' ,'dep':[] },
    4: {'tip':'一键配置:rosdep (国内版,解决ROS包的依赖关系的工具)',                 'type':2,    'tool':url_prefix+'tools/tool_config_rosdep.py' ,'dep':[] },
    5: {'tip':'一键配置:系统源 (恢复默认系统源)',           'type':2,    'tool':url_prefix+'tools/tool_config_system_source.py' ,'dep':[1] },
    6: {'tip':'一键安装:Docker',      'type':0,     'tool':url_prefix+'tools/tool_install_docker.py' ,'dep':[] },
    7: {'tip':'一键安装:nodejs',      'type':0,     'tool':url_prefix+'tools/tool_install_nodejs.py' ,'dep':[] },
    8: {'tip':'一键安装:VsCode开发工具',      'type':0,     'tool':url_prefix+'tools/tool_install_vscode.py' ,'dep':[] },
    9: {'tip':'一键安装:Cartographer(内测版v0.1)',      'type':0,     'tool':url_prefix+'tools/tool_install_cartographer.py' ,'dep':[4] },
    10: {'tip':'一键安装:微信 (Linux版本微信)',      'type':0,     'tool':url_prefix+'tools/tool_install_wechat.py' ,'dep':[6] },
    11: {'tip':'一键安装:github桌面版',             'type':0,     'tool':url_prefix+'tools/tool_install_github_desktop.py' ,'dep':[] },
    12: {'tip':'一键安装:PlateformIO MicroROS开发环境 (支持Fishbot)',      'type':0,     'tool':url_prefix+'tools/tool_install_micros_fishbot_env.py' ,'dep':[] },
    13: {'tip':'一键安装:Qt-Creator (板卡端安装,Qt版本5.11.3)',      'type':0,     'tool':url_prefix+'tools/tool_config_qt_creator.py' ,'dep':[] },
    14: {'tip':'一键安装:Qt-Creator (PC端安装)',      'type':0,     'tool':url_prefix+'tools/tool_config_pc_qt_creator.py' ,'dep':[] },
    15: {'tip':'一键配置:Qt交叉编译环境 (PC端配置)',      'type':2,     'tool':url_prefix+'tools/tool_config_pc_qtenv.py' ,'dep':[14] },
    16: {'tip':'一键安装:opencv (版本4.7.0.68)',      'type':0,     'tool':url_prefix+'tools/tool_config_opencv.py' ,'dep':[] },
    17: {'tip':'一键安装:RKNN-Toolkit2 (PC端安装)',      'type':0,     'tool':url_prefix+'tools/tool_config_pc_rknn.py' ,'dep':[] },
    18: {'tip':'一键安装:RKNN Toolkit Lite2 (板卡安装)',      'type':0,     'tool':url_prefix+'tools/tool_config_rknn.py' ,'dep':[] },
    # 76: {'tip':'一键安装:Code-Server(网页版VsCode)',      'type':0,     'tool':url_prefix+'tools/tool_install_code_server.py' ,'dep':[] },
    77: {'tip':'测试模式: 运行自定义工具测试'},
    99: {'tip':'关于',      'type':2,     'tool':url_prefix+'tools/tool_config_info.py' ,'dep':[] },
}
# 

def select():
    # download tools
    choose_dic = {}
    for tool_id in tools.keys(): choose_dic[tool_id]  = tools[tool_id]["tip"]
    code,result = ChooseTask(choose_dic, "---众多工具，等君来用---").run()
    if code==0: PrintUtils().print_success("已退出安装~")
    elif code==77: 
        code,result = ChooseTask(choose_dic, "请选择你要测试的程序:").run()
        if  code<0 and code>=77:  return False
        # CmdTask("cp tools/* /tmp/fishinstall/tools/").run
        run_tool_file(tools[code]['tool'].replace(url_prefix,'').replace("/","."))
    else: 
        download_tools(code,tools)
        run_tool_file(tools[code]['tool'].replace(url_prefix,'').replace("/","."))

    config_helper.gen_config_file()
    PrintUtils.print_delay("感谢您使用一键安装脚本~",0.1)

def main():
    # download base
    os.system("python3 tools/base.py")
    PrintUtils.print_delay(f"检测到你的系统版本信息为{distribution} {osversion.get_codename()},{osarch}",0.0001) 

    # check base config
    if not encoding_utf8:
        print("Your system encoding not support ,will install some packgaes..")
        CmdTask("sudo apt-get install language-pack-zh-hans -y",0).run()
        CmdTask("sudo apt-get install apt-transport-https -y",0).run()
        FileUtils.append("/etc/profile",'export LANG="zh_CN.UTF-8"')
        print('Finish! Please Try Again!')
        print('Solutions: https://fishros.org.cn/forum/topic/24 ')
        return False
    PrintUtils.print_success("基础检查通过...")

    select()

#    tip = """===============================================================================
#===========欢迎使用一键安装工具，人生苦短，三省吾身，省时省力省心!=============
#==============该工具由Embedfire根据fishros项目进行修改适配=====================
#================原开源地址：https://github.com/fishros/========================
#===============================================================================
#    """
#    PrintUtils.print_delay(tip,0.0001)


if __name__=='__main__':
    main()